"""
逃逸目标围捕示例
"""

import numpy as np
from usv_boids.core.simulation import Environment, SimulationEngine
from usv_boids.core.entities import Target, USV
from usv_boids.utils.visualization import Visualizer, Plotter


def main():
    print("启动逃逸目标围捕示例...")
    
    # 创建环境
    env = Environment(width=120.0, height=120.0, boundary_mode='wrap')
    sim = SimulationEngine(env)
    
    # 添加逃逸目标
    target = Target(
        position=np.array([60.0, 60.0]),
        velocity=np.array([0.3, -0.2]),
        max_speed=0.7,
        size=1.8,
        is_fleeing=True
    )
    sim.add_target(target)
    
    usv_count=12
    center_x, center_y = 60.0, 60.0
    radius = 30.0
    
    for i in range(usv_count):
        # 环形分布
        angle = 2 * np.pi * i / usv_count
        pos_x = center_x + radius * np.cos(angle)
        pos_y=center_y + radius * np.sin(angle)
        
        # 初始速度
        vel_x = -np.cos(angle) * 0.8
        vel_y=-np.sin(angle) * 0.8
        
        # 创建USV
        usv = USV(
            position=np.array([pos_x, pos_y]),
            velocity=np.array([vel_x, vel_y]),
            perception_radius=30.0,
            max_speed=1.8,
            max_force=0.5
        )
        sim.add_usv(usv)
    
    # 配置无人艇
    for usv in sim.usvs:
        usv.separation_weight = 1.5
        usv.alignment_weight=0.6
        usv.cohesion_weight = 0.6
        usv.target_weight=3.0
        
    vis = Visualizer(sim, show_metrics=True, figsize=(12, 8))
    vis.animate(frames=350, interval=50)
    
    report = Plotter.generate_report(sim)
    print(report)
    # Plotter.plot_metrics(sim, save_path="fleeing_target_metrics.png")


if __name__ == "__main__":
    main() 